<?php


function plan_test()
{
	ein("TEST 1: STG Framework");

	$srv = new stg_Server();

	$srv->create_user('icalvo');
	$srv->create_user('amartin');
	$srv->create_user('mpozo');
	$srv->create_user('ccalvo');

	$game = $srv->create_game('My game', 'Milky Way');

	$universe = $game->universe;

	// Universe objects creation
	$universe->add_object(new plan_universe_Planet('Earth', new stg_Point()));
	$universe->add_object(new plan_universe_Planet('Mars', new stg_Point()));

	// Player creation
	$universe->add_player(new stg_Player('Mandel', $srv->users['icalvo'], new stg_Faction()));
	$universe->add_player(new stg_Player('Narval', $srv->users['amartin'], new stg_Faction()));

	// Object ownerships
	$universe->objects['Earth']->add_owner($universe->players['Mandel']);
	$universe->objects['Mars']->add_owner($universe->players['Narval']);

	// Player configuration
	//$universe->players['Player 1'];

	// Turn zero execution
	$srv->games['My game']->execute_turn();

	// Players generate their movements
	
	$movement = <<<EOD
[Earth]->do_something(3,4)
[Mars]->do_something(5,6)
EOD;

function parse_movement($movement)
{
	$result = array();
	$commands = explode('\n', $movement);
	$n = 1;
	foreach ($commands as $cmd) {
		$errors = '';
		preg_match('/^\[([a-zA-Z ]+)\]->([a-zA-Z_]+) *\(([^)]*)\)/',
	    $movement, 
	    $matches);
	    if (count($matches) < 2) {
			$errors .= "Parse error, line $n";
		}
		$obj = $matches[1];
		$act = $matches[2];
		if ($matches[3])
		{
			$args = $matches[3];
			$args = split(',', $args);
			$args2 = array();
			foreach ($args as $arg) {
				if (is_numeric($arg)) {
					$args2[] = 0 + $arg;
				}
				elseif ($arg == 'TRUE') {
					$args2[] = TRUE;
				}
				elseif ($arg == 'FALSE') {
					$args2[] = FALSE;
				}
				elseif (substr($arg,0,1) == '[' AND substr($arg,strlen($arg)-1,1) == ']') {
					$objname = substr($arg,1,strlen($arg)-2);
					if (array_key_exists($objname, $this->universe->objects)) {
						$args2[] = $this->universe->objects[substr($arg,1,strlen($arg)-2)];
					}
					else
					{
						$errors .= "Object $objname doesn't exist";
					}
				}
				elseif (substr($arg,0,1) == "'" AND substr($arg,strlen($arg)-1,1) == "'") {
					$args2[] = substr($arg,1,strlen($arg)-2);
				}
				elseif (substr($arg,0,1) == '"' AND substr($arg,strlen($arg)-1,1) == '"') {
					$args2[] = substr($arg,1,strlen($arg)-2);
				}
				else {
					$errors .= "Argument type not recognized";
				}
			}
		}
		else {
			$args2 = null;
		}
		
		$n++;
	}
	
	if (FALSE)
	{
		throw new Exception($errors);
	}
}
	// Players send their movements

	$universe->players['Mandel']->add_command('Earth', 'do_something', array(3,4));
	$universe->players['Mandel']->add_command('Mars', 'do_something', array(5,6));
	
	// Turn One execution
	$srv->games['My game']->execute_turn();

	$srv->delete_user('icalvo');

	eout("TEST 1 ENDED");
}

?>

